<template>
    <div>
        <div class='content'>
            
            <el-button type="primary" @click="immediately" size="small" style="margin: 20px 0px 10px 20px;">立即执行规则</el-button>
            <el-button type="primary" @click="add" size="small" style="margin: 20px 0px 10px 20px;">添加规则</el-button>
            <!-- 新增 -->
            <el-dialog :title="dialogtitle" :visible.sync="centerDialogVisible" width="600px" center>
                <el-form :model="form" :rules="rules" ref="form" label-width="80px" class="demo-ruleForm" size="mini">
                    <el-form-item label="转移类型" prop="a_move_type">
                        <el-select v-model="form.a_move_type" placeholder="请选择" @change="typechange">
                            <el-option v-for="(item,index) in options.move_type" :key="index" :label="item" :value="index"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="部门" prop="a_d_id">
                        <el-select v-model="form.a_d_id" placeholder="请选择">
                            <!-- <el-option v-for="(item,index) in optionName" :key="index" :label="item.name" :value="item.id"></el-option> -->
                        <el-option-group v-for="group in optionName" :key="group.label" :label="group.label">
                            <el-option v-for="item in group.options" :key="item.value" :label="item.label" :value="item.value">
                            </el-option>
                        </el-option-group>
                        </el-select>
                    </el-form-item>
                    <div>
                        <el-form-item label="日期" prop="a_date_type">
                            <el-select v-model="form.a_date_type" placeholder="请选择">
                                <el-option v-for="(item,index) in opt_date_type" :key="index" :label="item.name" :value="item.id"></el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="超过" prop="a_day_num">
                            <el-input v-model="form.a_day_num" type="number" placeholder="例：1" style="width: 100px;margin-right: 15px;"></el-input>天
                        </el-form-item>
                    </div>
                    <el-form-item label="交易" prop="a_trade_type">
                        <el-select v-model="form.a_trade_type" placeholder="请选择">
                            <el-option label="全部" value="0"></el-option>
                            <el-option v-for="(item,index) in transaction" :key="index" :label="item.st_name" :value="item.st_id"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="状态" prop="a_m_status">
                        <el-select v-model="form.a_m_status" placeholder="请选择">
                            <el-option label="全部" value="0"></el-option>
                            <el-option v-for="(item,index) in status" :key="index" :label="item.st_name" :value="item.st_id"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="用途" prop="a_use_type">
                        <el-select v-model="form.a_use_type" placeholder="请选择">
                            <el-option label="全部" value="0"></el-option>
                            <el-option v-for="(item,index) in use" :key="index" :label="item.st_name" :value="item.st_id"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="委托" prop="a_entrust_way">
                        <el-select v-model="form.a_entrust_way" multiple placeholder="请选择"  @change="$set(form, form.a_entrust_way, $event)">
                             <el-option label="全部" value="0"></el-option>
                            <el-option v-for="(item,index) in entrust" :key="index" :label="item.st_name" :value="item.st_id"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="规则状态" prop="a_status">
                        <el-select v-model="form.a_status" placeholder="请选择">
                            <el-option v-for="(item,index) in opt_status" :key="index" :label="item.name" :value="item.id"></el-option>
                        </el-select>
                    </el-form-item>

                </el-form>
                <span slot="footer" class="dialog-footer">
                    <el-button @click="cancel('form')" size="mini">取 消</el-button>
                    <el-button type="primary" @click="submit('form')" size="mini">确 定</el-button>
                </span>
            </el-dialog>
            <!-- 列表 -->
            <div class='list'>
                <el-table ref="multipleTable" :data="list" tooltip-effect="dark" style="width: 100%">

                    <el-table-column align="center" prop="a_move_type_name" label="规则类型"></el-table-column>
                    <el-table-column align="center" prop="name" label="部门"></el-table-column>
                    <el-table-column align="center" prop="a_trade_type_name" label="交易"></el-table-column>
                    <el-table-column align="center" prop="a_m_status_name" label="房源状态"></el-table-column>
                    <el-table-column align="center" prop="a_use_name" label="用途"></el-table-column>
                    <el-table-column align="center" prop="a_status_name" label="规则状态"></el-table-column>
                    <el-table-column align="center" prop="a_date_type_name" label="日期"></el-table-column>
                    <el-table-column align="center" prop="a_created_at" label="创建时间"></el-table-column>
                    <el-table-column align="center" prop="a_updated_at" label="修改时间"></el-table-column>

                    <el-table-column fixed="right" label="操作" width="120">
                    <template slot-scope="scope">
                        <el-button @click.native.prevent="edit(scope.row)" type="text" size="small">编辑</el-button>
                        <el-button @click.native.prevent="deltest(scope.row)" type="text" size="small">删除</el-button>
                    </template>
                    </el-table-column>
                </el-table>
            </div>
        </div>
    </div>
</template>
<style>
    .home .view-content{
        width: 87%;
    }
</style>

<script>
    import conf from '../../../config/userconf.js'
  export default {
    data() {
        return {
            list:[],
            form:{
                a_d_id: '',
                a_move_type: 0,
                a_date_type: '',
                a_day_num: '0',
                a_trade_type: '0',
                a_m_status: '0',
                a_use_type: '0',
                a_entrust_way: [],
                a_status: '1',
            },
            optionName:[],  //部门名称
            rules:{
                a_d_id: [
                    { required: true, message: '请选择部门', trigger: 'change' }
                ],
                a_move_type: [
                    { required: true, message: '请选择转移类型', trigger: 'change' }
                ],
                a_date_type: [
                    { required: true, message: '请选择日期', trigger: 'change' },
                ],
                a_day_num: [
                    { required: true, message: '请输入超过天数', trigger: 'blur' }
                ],
                a_trade_type: [
                    { required: false, message: '请选择交易类型', trigger: 'change' }
                ],
                a_m_status: [
                    { required: false, message: '请选择房源状态', trigger: 'change' }
                ],
                a_use_type: [
                    { required: false, message: '请选择用途', trigger: 'change' }
                ],
                // a_entrust_way: [
                //     { required: true, message: '请选择委托方式', trigger: 'change' }
                // ],

            },
            dialogtitle:'添加规则',
            centerDialogVisible:false,
            bmId: '',  //规则id
            options: '',  //下拉框数据
            opt_status: [
                {name: '禁用', id: '0'},
                {name: '启用', id: '1'},
            ],
            opt_date_type: [
                {name: '最后跟进日期', id: '0'},
                {name: '委托日', id: '1'},
                {name: '录入日', id: '2'},
            ],
            transaction: '',  //交易
            status: '',  //状态
            use: '',  //用途
            entrust: '',  //委托
        }
    },
    methods: {
        /* 列表*/
        getList (data=null){
            conf.common.rulelist(data,(state,res)=>{
                if (state == 'success'&&res.data.code == 0){
                    console.log(res);
                    this.list = res.data.data   
                }else if(state == 'success'&&res.data.code != 0){
                    if(res.data.code == 401){
                        this.$router.push({name:'login'});   //登陆态失效，跳转至登录页
                    }else if(res.data.code == -404){
                        this.list = [];
                    }else{
                        this.$message({
                            type: 'info',
                            message: res.data.msg
                        });
                    }
                }
            })
        },
        /* 添加、修改 下拉数据*/
        ruleoptdata (data=null){
            conf.common.ruleoptdata(data,(state,res)=>{
                if (state == 'success'&&res.data.code == 0){
                    // console.log(res);
                    this.options = res.data.data; 
                    this.transaction =  res.data.data.r_transaction;
                    this.status =  res.data.data.r_status; 
                    this.use =  this.options.r_use;
                this.entrust =  this.options.r_delegation;
                }else if(state == 'success'&&res.data.code != 0){
                    if(res.data.code == 401){
                        this.$router.push({name:'login'});   //登陆态失效，跳转至登录页
                    }else{
                        this.$message({
                            type: 'info',
                            message: res.data.msg
                        });
                    }
                }
            })
        },
        /*取消 */
        cancel (formName) {
            this.centerDialogVisible = false;
            this.$refs[formName].resetFields();
        },
        /*提交 */
        submit (formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.judgeprimise();
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        /*判断提交 */
        judgeprimise (data=Object()) {
            if (this.dialogtitle == '添加规则') {
                conf.common.addrule(this.form,(state,res)=>{
                    if (state == 'success'&&res.data.code == 0){
                        this.getList();
                        this.centerDialogVisible = false;
                        this.$message({
                            type: 'success',
                            message: '添加成功!'
                        });
                        this.$refs['form'].resetFields();
                    }else if(state == 'success'&&res.data.code != 0){
                        if(res.data.code == 401){
                            this.$router.push({name:'login'});   //登陆态失效，跳转至登录页
                        }else{
                            this.$message({
                                type: 'info',
                                message: res.data.msg
                            });
                        }
                    }
                })
            }else if (this.dialogtitle == '修改规则'){

                data.a_id = this.bmId;
                data.a_d_id = this.form.a_d_id;
                data.a_date_type = this.form.a_date_type;
                data.a_day_num = this.form.a_day_num;
                data.a_entrust_way = this.form.a_entrust_way;
                data.a_m_status = this.form.a_m_status;
                data.a_move_type = this.form.a_move_type;
                data.a_status = this.form.a_status;
                data.a_trade_type = this.form.a_trade_type;
                data.a_use_type = this.form.a_use_type;
                conf.common.editrule(data,(state,res)=>{
                    if (state == 'success'&&res.data.code == 0){
                        this.getList();
                        this.centerDialogVisible = false;
                        this.$message({
                            type: 'success',
                            message: '修改成功!'
                        });
                        this.$refs['form'].resetFields();
                    }else if(state == 'success'&&res.data.code != 0){
                        if(res.data.code == 401){
                            this.$router.push({name:'login'});   //登陆态失效，跳转至登录页
                        }else{
                            this.$message({
                                type: 'info',
                                message: res.data.msg
                            });
                        }
                    }
                })
            }
        },
        /*增 */
        add (formName) {
            this.dialogtitle = '添加规则';
            this.centerDialogVisible = true;
        },
        // 删除规则
        deltest (row,data=Object()) {
            console.log(row);
            this.$confirm('确认彻底删除该规则吗 ? 删除后不可以恢复哦~', '删除规则记录', {  
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {

                data.a_id = row.a_id;
                conf.common.delrule(data,(state,res)=>{
                    if (state == 'success'&&res.data.code == 0){
                        this.getList();
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                        });
                    }else if(state == 'success'&&res.data.code != 0){
                        if(res.data.code == 401){
                            this.$router.push({name:'login'});   //登陆态失效，跳转至登录页
                        }else{
                            this.$message({
                                type: 'info',
                                message: res.data.msg
                            });
                        }
                    }
                })

            }).catch(() => {
                this.$message({
                  type: 'info',
                  message: '已取消删除'
                });          
            });
        },
        /**改*/
        edit (val) {
            // console.log(val);
            this.dialogtitle = '修改规则';
            this.form = {
                a_d_id: val.a_d_id,
                a_move_type: val.a_move_type,
                a_date_type: val.a_date_type.toString(),
                a_day_num: val.a_day_num,
                a_trade_type: val.a_trade_type,
                a_m_status: val.a_m_status,
                a_use_type: val.a_use_type,
                a_status: val.a_status,
            };
            let arr = [];
            for(var i=0;i<val.a_entrust_way.length;i++){
                arr.push(parseInt(val.a_entrust_way[i].id));
            }
            this.form.a_entrust_way = arr;
            this.bmId = val.a_id;
            this.centerDialogVisible = true;
        },
        //转移类型  change(类型不同，所对应的交易类型和状态也不同)
        typechange(){
            if(this.form.a_move_type == 0 || this.form.a_move_type == 2){
                this.form.a_trade_type = '0';
                this.form.a_m_status = '0';
                this.form.a_use_type = '0';
                this.form.a_entrust_way = [];
                this.transaction =  this.options.r_transaction;
                this.status =  this.options.r_status;
                this.use =  this.options.r_use;
                this.entrust =  this.options.r_delegation;
            }else if(this.form.a_move_type == 1){
                this.form.a_trade_type = '0';
                this.form.a_m_status = '0';
                this.form.a_use_type = '0';
                this.form.a_entrust_way = [];
                this.transaction =  this.options.c_transaction;
                this.status =  this.options.c_status;
                this.use =  this.options.c_use;
                this.entrust =  this.options.signing;
            }
        },
        //立即执行规则
        immediately(data=null){
            this.$confirm('是否开始执行所有【规则】（被禁用的不执行）?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                conf.common.implementrule(data,(state,res)=>{
                    if (state == 'success'&&res.data.code == 0){
                        console.log(res);
                        this.$message({
                            type: 'success',
                            message: '执行成功!'
                        });  
                    }else if(state == 'success'&&res.data.code != 0){
                        if(res.data.code == 401){
                            this.$router.push({name:'login'});   //登陆态失效，跳转至登录页
                        }else{
                            this.$message({
                                type: 'info',
                                message: res.data.msg
                            });
                        }
                    }
                })
                
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消执行'
                });          
            });
        },
        //获取部门
        obtainName(data=Object()){
            conf.department.nameOpt(data,(state,res)=>{
                if (state == 'success'&&res.data.code == 0){
                    this.optionName = res.data.data;
                    // console.log(res); 
                }else if(state == 'success'&&res.data.code != 0){
                    if(res.data.code == 401){
                        this.$router.push({name:'login'});   //登陆态失效，跳转至登录页
                    }else if(res.data.code == -404){
                        this.optionName = [];
                    }else{
                        this.$message({
                            type: 'info',
                            message: res.data.msg
                        });
                    }
                }
            })
        },
    },
    filters: {
        
    },
    mounted (){
        this.$nextTick(function(){
            this.getList();
            this.ruleoptdata();  //下拉框数据
            this.obtainName();  //部门
        })
    },
        
  }
</script>